"""
*****街道的每月排查数据,堆叠条形图,一次画3个，一共画7次*****
"""

import matplotlib.pyplot as plt
import numpy as np

months = ["一月", "二月", "三月", "四月", "五月", "六月", "七月"]
data = [
    [81, 81, 76, 77, 138, 163, 124],  # 凌水
    [108, 106, 93, 86, 91, 56, 108],  # 七贤岭
    [26, 25, 25, 26, 86, 66, 93],  # 龙王塘
]
labels = ["凌水", "七贤岭", "龙王塘"]
colors = ["blue", "red", "green", "yellow", "purple"]
corrent_month = len(months)
sum_data = [sum(x) for x in data]

# 将数据转化为三个部门每月数据
stacked_data = list(zip(*data))
# 坐标索引
x_index = [1, 2, 3]
height = 0.8  # 堆积条形的宽度

# 绘制堆叠条形图

# 初始化左侧位置
left = np.zeros(3)
# 初始化颜色，蓝色
color = colors[0]

fig, axs = plt.subplots()  # 生产1个子图
for i, d in enumerate(stacked_data):
    if i == corrent_month - 1:
        # 当月，设定第二个label
        color = colors[1]
        v_bar = axs.barh(
            x_index,
            d,
            color=color,
            left=left,
            edgecolor="white",
            label=f"{corrent_month}月排查量",
            height=height,
        )
    elif i == 0:
        # 第一个月，设定第一个label
        v_bar = axs.barh(
            x_index,
            d,
            color=color,
            left=left,
            edgecolor="white",
            label=f"1-{corrent_month-1}月每个月排查量",
            height=height,
        )
    else:
        # 其他月份，无label
        v_bar = axs.barh(
            x_index,
            d,
            color=color,
            left=left,
            edgecolor="white",
            height=height,
        )

    # # 条上显示分子分母式，分子是当月数值，分母是累计值
    # left = [x + y for x, y in zip(left, d)]  # 堆积值累计计算
    # # 在每个柱子上显示数值
    # for j, v, k in zip(x_index, left, d):
    #     # x_index为纵轴坐标，left为累计堆积值, d是当前月份的值
    #     # 在条形上方添加文本
    #     plt.text(v - k / 2, j, f"{k}/{v}", ha="center", va="bottom", color="white")

    # 条上只显示当月数值
    # 在每个柱子上显示数值
    # for j, v, k in zip(x_index, left, d):
    #     # x_index为纵轴坐标，left为累计堆积值, d是当前月份的值
    #     # 在条形上方添加文本
    #     axs.text(v + k / 2, j, str(k), ha="center", va="bottom", color="white")
    axs.bar_label(v_bar, label_type="center", color="white")

    left = [x + y for x, y in zip(left, d)]  # 堆积值累计计算

# plt.plot(sum_data, x_index, color="black", marker="o", linestyle="--", label="累计")
# 画3条平行线，分别表示3个部门的累计值，x坐标固定，y坐标

for i, d in enumerate(sum_data):
    axs.plot(
        [d, d],
        [0, i + 1 - height / 2],
        color=colors[i],
        linestyle="--",
        label=f"{labels[i]}累计排查{d}件",
    )

# 添加图例
# # 获取所有图例的句柄和内容
# original_legend = axs.legend()
# original_handles = original_legend.legendHandles
# original_labels = [text.get_text() for text in original_legend.get_texts()]
# # 将其中0和2对调
# original_handles[0], original_handles[2] = original_handles[2], original_handles[0]
# original_labels[0], original_labels[2] = original_labels[2], original_labels[0]
# # 显示图例
# axs.legend(original_handles, original_labels, loc="upper right")
v_handles, v_labels = axs.get_legend_handles_labels()
# 调换0和2的顺序
v_handles[0], v_handles[2] = v_handles[2], v_handles[0]
v_labels[0], v_labels[2] = v_labels[2], v_labels[0]
# 显示图例
axs.legend(v_handles, v_labels)


# 添加标题和轴标签
axs.set_title(f"1-{corrent_month}月各街道矛盾纠纷排查量")
axs.set_ylabel("街道")
axs.set_ylim(0, 4)
axs.set_yticks(x_index, labels)
axs.set_xlabel("数量")
# axs.grid(True)

# 设置中文用楷体
plt.rcParams["font.sans-serif"] = ["KaiTi"]
plt.rcParams["axes.unicode_minus"] = False  # 解决负号'-'显示为方块的问题

# 显示图形
plt.show()
